package com.tong.controller;

import com.tong.dto.AdminDto;
import com.tong.entity.Admin;
import com.tong.result.Response;
import com.tong.result.ResponseResult;
import com.tong.service.AdminService;
import com.tong.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2021-07-29 15:12:52
 */
@RestController
@RequestMapping
public class AdminController {
    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;
    @Autowired
    private RedisUtil redisUtil;

    @DeleteMapping("/token/{token}")
    public ResponseResult logout(@PathVariable("token") String token) {
        redisUtil.del(token);
        return Response.makeOKRsp();
    }

    /**
     * 获取用户信息
     *
     * @param token
     * @return
     */
    @GetMapping("admin-user")
    public ResponseResult admin(String token) {
        Admin admin = (Admin) redisUtil.get(token);
        return Response.makeOKRsp(admin);
    }

    /**
     * 登录
     *
     * @param admin
     * @param session
     * @return
     */
    @PostMapping("tokens")
    public ResponseResult tokens(@RequestBody Admin admin, HttpSession session) {
        log.info("接收到的admin对象为：{}", admin.toString());
        Admin adminDB = adminService.login(admin);
        String token = session.getId();
        redisUtil.set(token, adminDB, 30);
        return Response.makeOKRsp(token);
    }
}